using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Common;
using device;
using Microsoft.Win32;

namespace MainView.Test
{
	public partial class UCTest : UserControl, IComponentConnector
	{
		public UCTest()
		{
			InitializeComponent();
			listBoxData.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("TimeOutData")
			{
				Source = DeviceManager.GetInstance()
			});
		}

		private void btnComInfo_Click(object sender, RoutedEventArgs e)
		{
			DeviceSelectorWindow deviceSelectorWindow = new DeviceSelectorWindow();
			deviceSelectorWindow.ShowDialog();
		}

		private void checkBoxMonitor_Checked(object sender, RoutedEventArgs e)
		{
			DeviceManager.GetInstance().Interval = Convert.ToInt32(numericBoxInterval.Text);
			DeviceManager.GetInstance().StartTestResource();
		}

		private void checkBoxMonitor_Unchecked(object sender, RoutedEventArgs e)
		{
			DeviceManager.GetInstance().StopTestResource();
		}

		private void buttonClear_Click(object sender, RoutedEventArgs e)
		{
			DeviceManager.GetInstance().ClearTimeOutData();
		}

		private void buttonSave_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "txt 文件|*.txt";
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.InitialDirectory = Utility.GetMyDocumentFolder();
			if (saveFileDialog.ShowDialog() != true)
			{
				return;
			}
			StringBuilder stringBuilder = new StringBuilder();
			foreach (int timeOutDatum in DeviceManager.GetInstance().TimeOutData)
			{
				stringBuilder.AppendLine(timeOutDatum.ToString());
			}
			File.WriteAllText(saveFileDialog.FileName, stringBuilder.ToString());
		}
	}
}
